package com.spark.colliery.module.mine

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import android.widget.ImageView
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.spark.colliery.MainFragment
import com.spark.colliery.R
import com.spark.check.StdChkHistoryFragment
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.colliery.common.ext.*
import com.spark.data.NotifyHistoryFragment
import com.spark.data.PersonalFolderFragment
import com.spark.colliery.module.home.model.OptionsItemModel
import com.spark.colliery.module.mine.activity.ProfileActivity
import com.spark.trouble.HTHistoryFragment
import com.spark.common.ext.alert
import com.spark.common.ext.info
import com.spark.common.ext.server
import com.spark.rectify.RectHistoryFragment
import kotlinx.android.synthetic.main.fragment_mine.*

/**
 * Mine
 */

class MineFragment : BaseFragment() {

    companion object {

        fun newInstance(): MineFragment {
            return MineFragment()
        }
    }

    override fun contentResId(): Int {
        return R.layout.fragment_mine
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // personal profile
        iv_arrow.setOnClickListener { activity?.apply { ProfileActivity.actionStart(this) } }
        // optioins
        val mineOptionsAdapter = MineOptionsAdapter(R.layout.item_mine_options).apply {
            addData(OptionsItemModel.generateMineData())
            setOnItemClickListener { adapter, view, position ->
                val data = adapter.data[position] as OptionsItemModel
                when (data.name) {
                // "文件传输"
                 "纠违处理" -> {
                     parentFragment.let { it as MainFragment }.start(RectHistoryFragment.newInstance())
                 }
                // "标化工作"
                // "协商函"
                // "文件借阅"
                // "会议纪要"
                    "标化检查" -> {
                        parentFragment.let { it as MainFragment }.start(StdChkHistoryFragment.newInstance())
                    }
                    "隐患排查" -> {
                        parentFragment.let { it as MainFragment }.start(HTHistoryFragment.newInstance())
                    }
                    else -> {
                        alert("功能开发中，敬请期待!")
                    }
                }
            }
        }
        recy_mine_options.apply {
            layoutManager = object : GridLayoutManager(context, 4) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            adapter = mineOptionsAdapter
        }

        // 个人资料夹
        rl_personal_folder.setOnClickListener { parentFragment.let { it as MainFragment }.start(PersonalFolderFragment.newInstance()) }
        // 历史通知
        rl_notify_history.setOnClickListener { parentFragment.let { it as MainFragment }.start(NotifyHistoryFragment.newInstance()) }
        // 待办统计
        rl_todo_summary.setOnClickListener { alert("功能开发中，敬请期待!") }
        // 学习统计
        rl_study_summary.setOnClickListener { alert("功能开发中，敬请期待!") }
        // 帮助反馈
        rl_help_feed.setOnClickListener { alert("功能开发中，敬请期待!") }

        ntv_sanwei.setNum(12)
    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        Const.getUser()?.apply {
            if (!user.pictureurl.isNullOrBlank())
                civ_avatar.server(user.pictureurl)
            tv_user_name.text = user.name
            tv_phone.text = user.mobile

            fl_phone.double(onDoubleClickListener = object : OnDoubleClickListener {
                override fun onDoubleClicked() {
                    activity?.apply {
                        val cnm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                        val cd = ClipData.newPlainText("Label", tv_phone.text.toString().trim())
                        cnm.primaryClip = cd
                        info("您的电话号码已复制")
                        LogUtils.e("「cnm」${cnm.primaryClip}")
                    }
                }
            })
        }
    }
}


class MineOptionsAdapter(layoutRes: Int) : BaseQuickAdapter<com.spark.colliery.module.home.model.OptionsItemModel, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: com.spark.colliery.module.home.model.OptionsItemModel?) {
        if (item == null) return
        helper?.apply {
            Glide.with(mContext).load(item.iconResId).into(getView(R.id.iv_mine_option) as ImageView)
            setText(R.id.tv_mine_option, item.name)

        }
    }

}